package a66weding.com.jiehuntong.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;

import java.util.ArrayList;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.CateguryChildTopViewPagerBean;
import a66weding.com.jiehuntong.beans.CateguryGridViewBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.TaoCanBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;


public class Categury_GridViewDetailsActivity extends SwipeBackActivity implements OnNetResultListener, View.OnClickListener {
    Context context =Categury_GridViewDetailsActivity.this;
    //测拉关闭的控件
    private SwipeBackLayout mSwipeBackLayout;

    private TextView btn_top_back_categury;
    private TextView tv_top_title;
    private ViewPager vp_categury_child_top_viewpager;

    private RadioGroup rg_categury_child_radiogroup;
    private RadioButton rb_categury_child_radiobutton1;
    private RadioButton rb_categury_child_radiobutton2;
    private RadioButton rb_categury_child_radiobutton3;
    private ViewPager vp_categury_child_bottom_viewpager;




    //婚礼服务的每个item进去顶部轮播图的数据
    private CateguryChildTopViewPagerBean categuryChildTopViewPagerData;
    //套餐页面的数据
    private TaoCanBean taoCanData;
    //婚礼服务的每个item进去的底部的viewpager中listview的集合
    private ArrayList arraylist;
    private ImageOptions imageOptions;
    private ListView taocan;
    private ListView anli;
    private ListView shangjia;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categury_child1);
        //设置activity的左划关闭
        mSwipeBackLayout = getSwipeBackLayout();
        int edgeFlag = SwipeBackLayout.EDGE_LEFT;
        mSwipeBackLayout.setEdgeTrackingEnabled(edgeFlag);

        initView();
        initData();
    }



    private void initView() {
        btn_top_back_categury = (TextView)findViewById(R.id.btn_top_back_categury);
        tv_top_title = (TextView)findViewById(R.id.tv_top_title);
        vp_categury_child_top_viewpager = (ViewPager)findViewById(R.id.vp_categury_child_top_viewpager);
        rg_categury_child_radiogroup = (RadioGroup)findViewById(R.id.rg_categury_child_radiogroup);
        rb_categury_child_radiobutton1 = (RadioButton)findViewById(R.id.rb_categury_child_radiobutton1);
        rb_categury_child_radiobutton2 = (RadioButton)findViewById(R.id.rb_categury_child_radiobutton2);
        rb_categury_child_radiobutton3 = (RadioButton)findViewById(R.id.rb_categury_child_radiobutton3);
        vp_categury_child_bottom_viewpager = (ViewPager)findViewById(R.id.vp_categury_child_bottom_viewpager);



        arraylist = new ArrayList();
        taocan = new ListView(context);
        anli = new ListView(context);
        shangjia = new ListView(context);
        arraylist.add(taocan);
        arraylist.add(anli);
        arraylist.add(shangjia);

        //设置监听
        btn_top_back_categury.setOnClickListener(this);
        rg_categury_child_radiogroup.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        vp_categury_child_bottom_viewpager.setAdapter(new MyBottomAdapter());
    }

    private void initData() {
        //默认选中套餐
        rb_categury_child_radiobutton1.setChecked(true);

        int memberID=new User.DataBean().getI_Id();

        Intent intent=getIntent();
        int id=intent.getIntExtra("id",-1);
        String title=intent.getStringExtra("title");
        tv_top_title.setText(title);
        String topUrl= Url.CATEGURYGRIDVIEWTOPVIEWPAGER+id;
        String taocanUrl=Url.TAOCAN1+id+Url.TAOCAN2+memberID;
        String anliUrl=Url.ANLI+id;
        String shangjiaUrl=Url.SHANGJIA1+id+Url.SHANGJIA2;

        imageOptions = new ImageOptions.Builder()
                .setSize(DensityUtil.dip2px(30), DensityUtil.dip2px(40))
                .setCrop(false)
                .setImageScaleType(ImageView.ScaleType.FIT_XY)
                .setLoadingDrawableId(R.mipmap.ic_launcher)
                .setFailureDrawableId(R.mipmap.ic_launcher)
                .build();


        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.GETREQUEST, topUrl, this, CateguryGridViewBean.class, RequestCode.CATEGURY2);
        HttpHandleUtil.executeCommonGet(this,httpRequestBean);

        HttpRequestBean httpRequestBean2 = new HttpRequestBean(RequestCode.GETREQUEST, taocanUrl, this, CateguryGridViewBean.class, RequestCode.CATEGURY2);
        HttpHandleUtil.executeCommonGet(this,httpRequestBean2);

        HttpRequestBean httpRequestBean3 = new HttpRequestBean(RequestCode.GETREQUEST, anliUrl, this, CateguryGridViewBean.class, RequestCode.CATEGURY2);
        HttpHandleUtil.executeCommonGet(this,httpRequestBean3);

        HttpRequestBean httpRequestBean4 = new HttpRequestBean(RequestCode.GETREQUEST, shangjiaUrl, this, CateguryGridViewBean.class, RequestCode.CATEGURY2);
        HttpHandleUtil.executeCommonGet(this,httpRequestBean4);


    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof CateguryChildTopViewPagerBean) {
            categuryChildTopViewPagerData = (CateguryChildTopViewPagerBean) data;
            switch (categuryChildTopViewPagerData.getResCode()){
                case "0000":
                    //设置适配器
                    vp_categury_child_top_viewpager.setAdapter(new MyTopViewPager());

                    break;
            }

        }
        if(data instanceof TaoCanBean) {
            taoCanData = (TaoCanBean)data;
            switch (taoCanData.getResCode()){
                case "0000":
                    //设置适配器
                    taocan.setAdapter(new MyTaoCanAdapter());
                    break;
            }

        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_top_back_categury:
                finish();//销毁当前的activity
                break;
        }
    }

    private class MyTopViewPager extends PagerAdapter {
        @Override
        public int getCount() {
            return categuryChildTopViewPagerData.getData().size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(Categury_GridViewDetailsActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            String img_url = categuryChildTopViewPagerData.getData().get(position).getNvc_PicMain();
            Glide.with(Categury_GridViewDetailsActivity.this).load(img_url)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    private class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.rb_categury_child_radiobutton1:
                    vp_categury_child_bottom_viewpager.setCurrentItem(0);

                    break;
                case R.id.rb_categury_child_radiobutton2:
                    vp_categury_child_bottom_viewpager.setCurrentItem(1);
                    break;
                case R.id.rb_categury_child_radiobutton3:
                    vp_categury_child_bottom_viewpager.setCurrentItem(2);
                    break;

            }
        }
    }

    private class MyTaoCanAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return taoCanData.getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=null;
            if(convertView==null) {
                viewHolder=new ViewHolder();
                convertView=View.inflate(context,R.layout.item_categury_taocan,null);
                viewHolder.ib_taocan_topImage = (ImageButton)convertView.findViewById(R.id.ib_taocan_topImage);
                viewHolder.tv_taocan_topImage_details = (TextView)convertView.findViewById(R.id.tv_taocan_topImage_details);
                viewHolder.iv_logo_image = (ImageView) convertView.findViewById(R.id.iv_logo_image);
                viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                viewHolder.tv_newPrice = (TextView)convertView.findViewById(R.id.tv_newPrice);
                viewHolder.tv_oldPrice = (TextView) convertView.findViewById(R.id.tv_oldPrice);
                viewHolder.ib_taocan_panicbuyingv = (ImageButton) convertView.findViewById(R.id.ib_taocan_panicbuying);
                convertView.setTag(viewHolder);
            }else{
                viewHolder= (ViewHolder) convertView.getTag();
            }
            Glide.with(context).load(taoCanData.getData().get(position).getNvc_PicMain())
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(viewHolder.ib_taocan_topImage);
            viewHolder.tv_taocan_topImage_details.setText(taoCanData.getData().get(position).getNvc_Name());
            Glide.with(context).load(taoCanData.getData().get(position).getBusiness().getNvc_Logo())
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(viewHolder.iv_logo_image);
            viewHolder.tv_title.setText(taoCanData.getData().get(position).getBusiness().getNvc_StoreName());
            viewHolder.tv_newPrice.setText(taoCanData.getData().get(position).getDm_ShopPrice()+"￥");
            viewHolder.tv_oldPrice.setText(taoCanData.getData().get(position).getDm_Price()+"￥");
            viewHolder.tv_oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

            return convertView;
        }
    }

    class ViewHolder {
        //套餐页面的控件
        ImageButton ib_taocan_topImage;
        TextView tv_taocan_topImage_details;
        ImageView iv_logo_image;
        TextView tv_title;
        TextView tv_newPrice;
        TextView tv_oldPrice;
        ImageButton ib_taocan_panicbuyingv;
    }

    private class MyBottomAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return arraylist.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ListView view = (ListView) arraylist.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
